package Other;

/**
 * Created by Lee on 2017/3/7 0007.
 */
public class FourSplitTwo {

    public static void main(String[] args) {
//        int[] A={1,1,1,1,7,1,3,4,1,2,1,5,2,2};
        int[] A={1,1,1,1,10,1,3,1,1,2,1,5,2,2};
//        int[] A={1,1,1,4,3,3,3,4,1,1,1};
//        int[] A={2,5,1,1,1,1,4,3,7,5,7};
//        int[] A={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
//        int[] A={1,1,1};
//        int[] A={};
        Boolean res = solution(A);
        System.out.println(String.valueOf(res));
    }

    static boolean solution(int A[]){
        int v2=findMid(A,0,A.length-1);
        int v1=findMid(A,0,v2-1);
        int v3=findMid(A,v2+1,A.length-1);
        return check(A, v1, v2, v3);
    }
    static int findMid(int A[],int lo,int high){
        int[] s1 = new int[A.length];
        int[] s2 = new int[A.length];
        for(int i=lo;i<=high;i++) {
            if(i==lo){
                s1[i]=0+A[i];
            }
            else s1[i]=s1[i-1]+A[i];
        }
        for(int i=high;i>=lo;i--) {
            if(i==high){
                s2[i]=0+A[i];
            }
            else s2[i]=s2[i+1]+A[i];
        }
        for(int i=lo;i<=high;i++){
            if (s1[i]-s2[i]>=0){
                return s1[i]-s2[i]<s2[i-1]-s1[i-1]?i:i-1;
//                return i;
            }
        }
        return -1;
    }

    static boolean check(int[] A,int v1, int v2, int v3) {
        int s1=0,s2=0,s3=0,s4=0;
        for(int i=0;i<A.length;i++){
            if(i<v1){
                s1+=A[i];
            }else if (i>v1&&i<v2){
                s2+=A[i];
            }else if (i>v2&&i<v3){
                s3+=A[i];
            }else if (i>v3&&i<A.length){
                s4 += A[i];
            }
        }
        if (s1==s2&&s2==s3&&s3==s4){
            return true;
        }else return false;
    }
}
